package threading;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-04-01
 * Time: 9:56
 */
public class ThreadExample_wait_notify {
    public static void main(String[] args) throws InterruptedException {
        Object object = new Object();
        Thread t1 = new Thread(()->{
           synchronized (object) {
               try {
                   System.out.println("wait开始");
                   object.wait();
                   System.out.println("wait结束");
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });

        Thread t2 = new Thread(()->{
           synchronized (object) {
               System.out.println("notify开始");
               object.notify();
               //在这里加上一个sleep可以更清楚的看到，notify不解锁
               try {
                   Thread.sleep(3000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               System.out.println("notify结束");
           }
        });
        //等待t1中wait解锁阻塞，否则t2中的notify没有效果
        t1.start();
        Thread.sleep(1000);
        t2.start();
    }
}
